var datagrid = null;
var tableTree = null, clickTableId = null;
var expandRow = {};
$(function() {
	tableTree = $('#tableTree').treegrid({
		data: [],
		fit : true,
		fitColumns : true,
		border : false,
		pagination : false,
		autoRowHeight: false,
		striped: true,
		pageSize : 20,
		pageList : [ 10, 20, 30, 40, 50 ],
		nowrap : true,
		rownumbers: true,
		idField: 'id',
		treeField: 'text',
		frozenColumns: [],
		columns : [[
			{field : 'text', title : '表名', width : 120},
			{field : 'id', title : '操作', width : 120,
				formatter : function(value, row, index) {
					var oper = [];
					oper.push("<a href='javascript: void(0)' onclick='editTable(\"" + row.id + "\")'>修改</a>&nbsp;");
					if (!row.children) {
						oper.push("<a href='javascript: void(0)' onclick='loadTableField(\"" + row.id + "\")'>加载字段</a>&nbsp;");
						//oper.push("<a href='javascript: void(0)' onclick='setJoinTable(\"" + row.id + "\", \"" + row.name + "\", \"" + row.text + "\")'>关联表</a>");
					}
					return oper.join("");
				}
			}
		]],
		toolbar: [{
			text : '新增',
			iconCls : 'icon-add',
			handler : function() {
				editTable();
			}
		}],
		onClickRow: function(data) {
			if (data.children) {
				if (expandRow[data.id] == "open") {
					$('#tableTree').treegrid("collapse", data.id);
					expandRow[data.id] = "close";
				} else {
					$('#tableTree').treegrid("expand", data.id);
					expandRow[data.id] = "open";
				}
				$('#datagrid').datagrid("loadData", []);
			} else if (clickTableId != data.id) {
				serarchFun(data.id);
				clickTableId = data.id;
			}
			$("#addField").attr("disabled", false);
		}
	});
	initTree();
	initGrid();
});
function initTree() {
	$.post(host + 'tableAction/getTableList.html', {}, function(data) {
		data = data.rows;
		var nodes = [];
		var map = {};
		var push = {};
		for (var i = 0;i < data.length;i ++) {
			map[data[i].id] = data[i];
			if (map[data[i].parentId] != undefined) {
				if (map[data[i].parentId].children == undefined) {
					map[data[i].parentId].children = [];
					map[data[i].parentId].state = "closed";
				}
				map[data[i].parentId].children.push(map[data[i].id]);
				push[data[i].id] = true;
			}
		}
		for (var i = 0;i < data.length;i ++) {
			if (push[data[i].id])
				continue;
			if (map[data[i].parentId] != undefined) {
				if (map[data[i].parentId].children == undefined) {
					map[data[i].parentId].children = [];
					map[data[i].parentId].state = "closed";
				}
				map[data[i].parentId].children.push(map[data[i].id]);
			} else {
				nodes.push(map[data[i].id]);
			}
		}
		tableTree.treegrid("loadData", nodes);
	}, "json");
	$("#tableTree").tabs({});
}
function initGrid() {
	datagrid = $('#datagrid').datagrid({
		url : host + 'tableAction/getFieldList.html',
		fit : true,
		fitColumns : true,
		border : false,
		pagination : false,
		checkOnSelect : false,
		selectOnCheck : false,
		rownumbers: true,
		nowrap : false,
		frozenColumns: [],
		columns : [[
			{field : 'name', title : '字段名', width : 60},
			{field : 'text', title : '字段名称', width : 60},
			{field : 'typeId', title : '类型', width : 60},
			{field : 'isId', title : '主键', width : 60,
				formatter: function(value, row, index) {
					return value == 1 ? "是" : "否";
				}
			},
			{field : 'isOnly', title : '唯一键', width : 60,
				formatter: function(value, row, index) {
					return value == 1 ? "是" : "否";
				}
			},
			{field : 'isNotNull', title : '不能为空', width : 60,
				formatter: function(value, row, index) {
					return value == 1 ? "是" : "否";
				}
			},
			{field : 'id', title : '操作', width : 100,
				formatter : function(value, row, index) {
					return "<a href='javascript: void(0)' onclick='editField(\"" + value + "\")'>修改</a>&nbsp;"+
							"<a href='javascript: void(0)' onclick='deleteField(\"" + value + "\")'>删除</a>"; 
				}
			}
		]],
		toolbar: [{
			text : '新增',
			iconCls : 'icon-add',
			handler : function() {
				editField();
			}
		}]
	});
}
function setJoinTable(id, tableName, name) {
	dialog = baseJs.window(host + "system/setting/table/tableJoinManage.html?tableName=" + 
			tableName + "&name=" + name + "&tableId=" + id, {
		title: "关联表", width: 600, height: 400
	});
}
var load = null;
function loadTableField(id) {
	load = baseJs.loadding();
	$.post(host + "tableAction/loadTableField.html", {tableId: id}, function(data) {
		if (data.success) {
			serarchFun(id);
		} else {
			alert(data.msg);
		}
		load.close();
	}, "json");
}
var dialog = null;
function editTable(tableId) {
	var param = "";
	if (tableId) {
		param = "?id=" + tableId;
	}
	dialog = baseJs.window(host + "system/setting/table/editTable.html" + param, {
		title: "编辑表信息", width: 450, height: 230
	});
}
function editField(id) {
	var param = "";
	if (id) {
		param = "?id=" + id;
	} else {
		param = "?tableId=" + id;
	}
	dialog = baseJs.window(host + "system/setting/table/editField.html" + param, {
		title: "编辑字段信息", width: 480, height: 300});
}
function deleteField(id){
 	if (window.confirm("是否确认删除此字段？")) {
		$.post(host + "tableAction/deleteField.html", {"id": id},
			function(data) {
				if (data.status == "error") {
					alert(data.msg);
				}
				if (data.status == "ok") {
					datagrid.datagrid('reload');
					alert(data.msg);
				}
			},
		"json");
	}
}
function serarchFun(id) {
	datagrid.datagrid('load',{
		"tableId" : id ? id : ""
	});
}

function cleanFun() {
	$('#searchForm input').val('');
	datagrid.datagrid('load', {});
}